package com.whty.mongodemo.web;

import com.whty.mongodemo.route.CustomForm;
import com.whty.mongodemo.route.FormData;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HttpServerVerticle extends AbstractVerticle {

  private final int port = 8080;

  @Override
  public void start() {
    HttpServer server = vertx.createHttpServer();

    Router router = Router.router(vertx);

    router.route().handler(BodyHandler.create());
    //注册路由
    this.registerRoute(router);
    server.requestHandler(router).listen(port, ar -> {
      if (ar.succeeded()) {
        log.info("web服务启动成功，监听端口{}", port);
      } else {
        log.error("web服务启动失败");
      }
    });
  }

  private void registerRoute(Router router) {
    new CustomForm().init(router);
    new FormData().init(router);
  }
}
